package com.kiwilss.aopdemo.ui.lifecycler

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.kiwilss.aopdemo.utils.LogUtils


/**
 * @author : Administrator
 * e-mail : kiwilss@163.com
 * time   : 2021/02/01
 *  desc   :
 */
class MyObserver(var lifecycler: Lifecycle, var callback: Callback) : LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun connectOnCreate() {
        p("onCreate")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun connectOnResume() {
        p("onResume")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun disconnectOnDestroy() {
        p("onDestroy")
    }

    private fun p(string: String) {
        LogUtils.e(string)
        callback.update(string)
    }

}

interface Callback {
    fun update(string: String)
}